/**
* 构建 代理服务 镜像 功能
* 1 nginx
* 2 frpc
* 3 证书签发 certbot
* 4 域名解析
**/
#include <docker.h>


int saveProxyImage(string buildDir,const char *prefix){
    string file="FROM ubuntu:22.04 AS builder \n";
    // 配置数据源列表
    sources_list(file);

    file+="apt-get install -y git && \\ \n";
    file+="git clone -b main https://gitcode.com/hq_make/util-docker.git main && \\ \n";
    file+="cd main/shell && chmod +x *.sh && \\ \n";
    file+="./install.sh && cd ../ && rm -rf main && \\ \n";
    file+="util-docker -cmd build_nginx -prefix "+string(prefix)+" && \\ \n";
    file+="util-docker -cmd build_start_proxy -prefix "+string(prefix)+" && \\\n";
    // 复制程序到镜像
    string apptmp="/opt/myapp";
    file+="util-docker -cmd output_app -prefix "+apptmp+" -apppath /usr/local/bin/start && \\ \n";
    file+="util-docker -cmd output_app -prefix "+apptmp+" -apppath /usr/local/bin/util-docker && \\ \n";
    file+="util-docker -cmd output_app -prefix "+apptmp+" -apppath /usr/local/bin/frpc && \\ \n";
    file+="util-docker -cmd output_app -prefix "+apptmp+" -apppath /usr/local/sbin/nginx && \\ \n";
    file+="cp -p /usr/local/conf/mime.types "+apptmp+"/mime.types \n";

    // 运行镜像
    //util-docker -cmd input_app -prefix /opt/work
    file+="FROM ubuntu:22.04\n";
    file+="COPY --from=builder "+apptmp+" "+apptmp+"\n";
    file+="ENV DEBIAN_FRONTEND=noninteractive \n";
    sources_list(file);
    // 配置时区
    file+="apt-get install -y tzdata  && \\\n";
    file+="echo \"Asia/Shanghai\" > /etc/timezone  && \\\n";
    file+="dpkg-reconfigure -f noninteractive tzdata  && \\\n";
    file+="ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  && \\\n";

    file+="apt-get install -y python3-pip  && \\\n";
    file+="pip install certbot-dnspod && \\\n";
    file+="apt-get install -y git  && \\\n";
    file+="mkdir -p /usr/local/conf && cp -p "+apptmp+"/mime.types  /usr/local/conf/mime.types && \\\n";
    file+="git clone -b main https://gitcode.com/hq_make/util-docker.git /tmp/main &&\\\n";
    file+="cd /tmp/main/shell && chmod +x *.sh && \\ \n";
    file+="./install.sh && cd / && \\\n";
    file+="util-docker -cmd input_app -prefix "+apptmp+" && \\\n";

    file+="apt-get clean && \\\n";
    file+="rm -rf /var/lib/apt/lists/* /var/lib/apt/archives/* /tmp/* \n";
    file+="EXPOSE 80 443 \n";
    file+="ENTRYPOINT [\"/bin/bash\", \"-c\"] \n";
    file+="CMD [\"/usr/local/bin/start\"] \n";

    // 保存镜像
    string save_repository="crpi-vb403qyxdijc0eq9.cn-guangzhou.personal.cr.aliyuncs.com/utlis/build";
    // 保存镜像标签
    string save_tag="22_04_nginx_proxy_frpc";
    // 本地镜像
    string local_tag = "nginx_proxy_frpc";
    string local_repository = "ubuntu-22.04";


    return build_image(buildDir,file,local_repository,local_tag,save_repository,save_tag);
}

